home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / LIB / PRINTMSG.C < prev    next >
C/C++ Source or Header  |  1993-09-20  |  6KB  |  197 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    p r i n t m s g . c                                             */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*
  13.  *    $Id: printmsg.c 1.4 1993/09/20 04:38:11 ahd Exp $
  14.  *
  15.  *    $Log: printmsg.c $
  16.  *     Revision 1.4  1993/09/20  04:38:11  ahd
  17.  *     TCP/IP support from Dave Watt
  18.  *     't' protocol support
  19.  *     OS/2 2.x support
  20.  *
  21.  *     Revision 1.3  1993/04/10  21:26:04  ahd
  22.  *     Use unique buffer for printmsg() time stamp
  23.  *
  24.  * Revision 1.2  1992/11/20  12:39:37  ahd
  25.  * Move heapcheck to check heap *EVERY* call
  26.  *
  27.  */
  28.  
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. #ifdef __CORE__
  36. #define __HEAPCHECK__
  37. #endif
  38.  
  39. #ifdef __HEAPCHECK__
  40. #include <alloc.h>
  41. #else
  42. #ifdef __CORELEFT__
  43. #include <alloc.h>
  44. #endif
  45. #endif
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*                    UUPC/extended include files                     */
  49. /*--------------------------------------------------------------------*/
  50.  
  51. #include "lib.h"
  52. #include "dater.h"
  53. #include "logger.h"
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                          Global variables                          */
  57. /*--------------------------------------------------------------------*/
  58.  
  59. #ifdef __HEAPCHECK__
  60. currentfile();
  61. #endif
  62.  
  63. int debuglevel = 1;
  64. FILE *logfile = stdout;
  65.  
  66. #ifdef __CORE__
  67. long  *lowcore = NULL;
  68. char  *copyright = (char *) 4;
  69. char  *copywrong = NULL;
  70. #endif
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*    As this routine is called from everywhere, we turn on stack     */
  74. /*    checking here to handle the off-chance we screwed up and        */
  75. /*    blew the stack.  This may catch it late, but it will catch      */
  76. /*    it.                                                             */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. #ifdef __TURBOC__
  80. #pragma -N
  81. #else
  82. #pragma check_stack( on )
  83. #endif
  84.  
  85. char *full_log_file_name = "UUPC log file";
  86.  
  87. /*--------------------------------------------------------------------*/
  88. /*   p r i n t m s g                                                  */
  89. /*                                                                    */
  90. /*   Print an error message if its severity level is high enough.     */
  91. /*   Print message on standard output if not in remote mode           */
  92. /*   (call-in).  Always log the error message into the log file.      */
  93. /*                                                                    */
  94. /*   Modified by ahd 10/01/89 to check for Turbo C NULL pointers      */
  95. /*   being de-referenced anywhere in program.  Fixed 12/14/89         */
  96. /*                                                                    */
  97. /*   Modified by ahd 04/18/91 to use true variable parameter list,    */
  98. /*   supplied by Harald Boegeholz                                     */
  99. /*--------------------------------------------------------------------*/
  100.  
  101. void printmsg(int level, char *fmt, ...)
  102. {
  103.    va_list arg_ptr;
  104.  
  105. #ifdef __CORELEFT__
  106.    static unsigned freecore = 63 * 1024;
  107.    unsigned nowfree;
  108. #endif
  109.  
  110. #ifdef __HEAPCHECK__
  111.       static boolean recurse = FALSE;
  112.       int heapstatus;
  113.  
  114.       heapstatus = heapcheck();
  115.       if (heapstatus == _HEAPCORRUPT)
  116.          printf("\a*** HEAP IS CORRUPTED ***\a\n");
  117.  
  118. #endif
  119.  
  120. #ifdef __CORE__
  121.    if (*lowcore != 0L)
  122.    {
  123.       putchar('\a');
  124.       debuglevel = level;  /* Force this last message to print ahd   */
  125.    }
  126. #endif
  127.  
  128.  
  129. #ifdef __CORELEFT__
  130.    nowfree = coreleft();
  131.    if (nowfree < freecore)
  132.    {
  133.       freecore = (nowfree / 10) * 9;
  134.       printmsg(0,"Free memory = %u bytes", nowfree);
  135.    }
  136. #endif
  137.  
  138.    if (level <= debuglevel)
  139.    {
  140.  
  141.       FILE *stream = (logfile == NULL) ? stderr : logfile;
  142.  
  143.       va_start(arg_ptr,fmt);
  144.  
  145.       if ((stream != stdout) && (stream != stderr))
  146.       {
  147.          char now[DATEBUF];
  148.          vfprintf(stderr, fmt, arg_ptr);
  149.          fputc('\n',stderr);
  150.  
  151.          if ( debuglevel > 1 )
  152.             fprintf(stream, "(%d) ", level);
  153.          else
  154.             fprintf(stream, "%s ", dater( time( NULL ), now));
  155.  
  156.       } /* if (stream != stdout) */
  157.  
  158.       if (!ferror(stream))
  159.          vfprintf(stream, fmt, arg_ptr);
  160.  
  161.       if (!ferror(stream))
  162.          fputc('\n',stream);
  163.  
  164.       if (ferror(stream))
  165.       {
  166.          perror(full_log_file_name);
  167.          abort();
  168.       } /* if */
  169.  
  170. #ifdef __HEAPCHECK__
  171.       if ( !recurse )
  172.       {
  173.          recurse = TRUE;
  174. #ifdef __CORE__
  175.          if (*lowcore != 0L)
  176.             panic();
  177.     /*     if (!equal(copyright,copywrong))
  178.             panic();                         */
  179. #endif
  180.          if (heapstatus == _HEAPCORRUPT)
  181.             panic();
  182.          recurse = FALSE;
  183.       }
  184. #endif
  185.  
  186.  
  187. /*--------------------------------------------------------------------*/
  188. /*                        Massive debug mode?                         */
  189. /*--------------------------------------------------------------------*/
  190.  
  191.    if ((debuglevel > 10) &&  ((level+2) < debuglevel))
  192.       fflush( logfile );
  193.  
  194.    } /* if (level <= debuglevel) */
  195.  
  196. } /*printmsg*/
  197.